package com.lyncs.ods.utils;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author alex
 * @date 2022/6/1 22:48
 * @description
 */
@Slf4j
public class NotifyUtils {

    private String buildSmsContent(String content, Object... args) {
        List<Object> paramList = Lists.newArrayList();
        if (ArrayUtils.isNotEmpty(args)) {
            paramList.addAll(Arrays.stream(args).filter(Objects::nonNull).collect(Collectors.toList()));
        }
        return MessageFormat.format(content, paramList.toArray());
    }

    private String buildEmailContent(String path, Object... args) {
        String emailContent = getShareHtml(path);
        List<Object> paramList = Lists.newArrayList();
        if (ArrayUtils.isNotEmpty(args)) {
            paramList.addAll(Arrays.stream(args).filter(Objects::nonNull).collect(Collectors.toList()));
        }
        return MessageFormat.format(emailContent, paramList.toArray());
    }

    private String getShareHtml(String path) {
        try {
            ClassPathResource classPathResource = new ClassPathResource(path);
            InputStream inputStream = classPathResource.getInputStream();
            return new String(inputStream.readAllBytes());
        } catch (IOException e) {
            log.info("failed to load file content, path:{}", path);
            e.printStackTrace();
        }
        return "";
    }

    public static void main(String[] args) {
        String s = "{0} say {1}, fuck {0}";
        System.out.println(MessageFormat.format(s, List.of("SanZhang","XXX").toArray()));
    }
}
